package net.amsoft.iservice.management.webapp.struts.form;

import javax.servlet.http.HttpServletRequest;

import net.amsoft.iservice.management.util.IBrokerConstants;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LinksUploadForm extends ActionForm 
{
    private static Logger logger = Logger.getLogger(LinksUploadForm.class);
	private String[] name;
	private String[] address;
	private String action;
    boolean emptyName = false;
    boolean emptyAddress = false;

	public String[] getName() {
        logger.info("getName(): Returning name >> " + name);
	    return name;
	}
	public void setName(String name[]) {
        logger.info("setName(): Setting name >> " + name);
		this.name = name;
	}

	public String[] getAddress() {
        logger.info("getAddress(): Returning address >> " + address);
	    return address;
	}
	public void setAddress(String address[]) {
        logger.info("setAddress(): Setting address >> " + address);
		this.address = address;
	}
	  
    public String getAction() {
        logger.info("getAction(): Returning action >> " + action);
        return action;
    }

    public void setAction(String action) {
        logger.info("setAction(): Setting action >> " + action);
        this.action = action;
    }

    /**
     * Checks if user has entered an invalid file format
     */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
	{
		// Check for mandatory data
		ActionErrors errors = new ActionErrors();

		// Check if there is any input
		if ("upload".equals(action))
		{
			String error = checkEmpty();
		    if (error.length()>0)
		        errors.add("name", new ActionMessage(error));
		    else
		    {
		        // Check if input is valid
		        error = checkInvalid();
		        if (error.length()>0)
		            errors.add("name", new ActionMessage(error));
		    }
		}
	        
		return errors;
	}
	
	/**
	 * Checks if input is null
	 * @return error key
	 */
	private String checkEmpty()
	{
	    String error = "";
	    
		if ((name==null)||(name.length==0)||("".equals(name[0].trim())))
		    emptyName = true;
		
		if ((address==null)||(address.length==0)||("".equals(address[0].trim())))
		    emptyAddress = true;
		
		if (emptyName && emptyAddress)
		{
		    logger.error("checkEmpty(): Found null input");		 
			error = IBrokerConstants.LINKS_EMPTY;
		}
		return error;
	}

	/**
	 * Checks if input is valid
	 * @return error key
	 */
	private String checkInvalid()
	{
	    String error = "";

	    // Check if proper name-address pairs are there
	    for (int i=0; i<name.length; i++)
	    {
	        if (((!("".equals(name[i].trim())) && ("".equals(address[i].trim()))))
	            || (("".equals(name[i].trim()) && (!("".equals(address[i].trim()))))))
	        {
	            logger.error("checkInvalid(): Found improper name-address pairs");
	            error = IBrokerConstants.LINKS_INVALID;
	            break;
	        }
		}
	    return error;
	}
	
}